<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use app\index\jwt\Jwts;

class Main extends Controller {

    protected $sysset = array();
    protected $merchinfo = array();

    public $merch_id;

    protected $data = array();

     public function __construct() {
         $this->veToken();
//         $this->merchinfo = unserialize(base64_decode(session('merchinfo')));
         $this->sysset = get_sysset();
         $this->data = request()->param();
//        $_GPC = input();
//        if (session('merchinfo') == "") {
//            if ($this->request->isAjax()) {
//                json('error', 1, '登录超时！请刷新页面重新登录。');
//            } else {
//                $url = $this->request->domain(true) . '/index/login';
//                header('HTTP/1.1 303 See Other');
//                header("Location: $url");
//                exit;
//            }
//        } else {
//
//        }
//        $_W['baseurl']=$this->request->domain(true);
//         $_W['timestamp'] =time();
//         $_W['sysset']=$this->sysset;
//         $_W['sysset']['agent_level']=$this->merchinfo['agent_level'];
//         $this->view->assign('_W', $_W);
//
//
//         $this->view->assign('sysset', $this->sysset);
//        $this->view->assign('merchinfo', $this->merchinfo);
//        $this->view->assign('platform_name', $this->sysset['basic']['platform_name']);
//        $this->view->assign('logo', $this->sysset['basic']['logo']);
//        if (cookie("formtoken") == "") {
//            setcookie('formtoken', md5(rand(1, 999)), time() + 7200, '/');
//        }
    }

    /**
     * 验证token
     */
    private function veToken(){
        $header = request()->header();
        if(!isset($header['authorization'])){
            return json(['data'=>[],'code'=>401],200);
        }
        $tokendata = explode(' ',$header['authorization']);
        $token = $tokendata[1];
        $arr = Jwts::verifyToken($token);
        if(empty($arr)){
            return jsonp_error(401,'认证失败！');
        }
        $this->merch_id = $arr['data']->userid->id;
        $this->merchinfo = Db::name('member')->where(['id'=>$this->merch_id])->find();
    }

}
